package com.qf.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.mapper.RiskMapper;

import com.qf.pojo.Risk;
import com.qf.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
@RequestMapping("risk")
public class RiskController {

    @Autowired
    private RiskMapper riskMapper;

    @PostMapping("physical")
    public Result post(String score ,String count){
        Integer count1 = Integer.valueOf(count);
        String username=(String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        System.out.println(username);
        Risk risk = new Risk();
        risk.setName(username);
        switch (count1){
            case 0:
                risk.setYangxu_score(score);
                break;
            case 1:
                risk.setYinxu_score(score);
                break;
            case 2:
                risk.setQixu_score(score);
                break;
            case 3:
                risk.setTanshi_score(score);
                break;
            case 4:
                risk.setShire_score(score);
                break;
            case 5:
                risk.setXueyu_score(score);
                break;
            case 6:
                risk.setTebing_score(score);
                break;
            case 7:
                risk.setQiyu_score(score);
                break;
            case 8:
                risk.setPinghe_score(score);
                break;
        }

        LambdaQueryWrapper<Risk> qw = new LambdaQueryWrapper<>();
        qw.eq(Risk::getName,username);
        long count2 = riskMapper.selectCount(qw);
        if (count2 >0){
           riskMapper.update(risk,qw);
        }else {
           riskMapper.insert(risk);
        }

        return Result.success("success");
    }

    @PostMapping("riskscore")
    public Result risk(String score ){

        String username=(String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        Risk risk = new Risk();
        risk.setName(username);
        risk.setRisk_score(score);

        LambdaQueryWrapper<Risk> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(Risk::getName,username);
        long count = riskMapper.selectCount(qw1);
        if (count >0){
            riskMapper.update(risk,qw1);
        }else {
            riskMapper.insert(risk);
        }

        return Result.success("success");
    }

    @PostMapping("psychological")
    public Result psychological(String score ){

        String username=(String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        Risk risk = new Risk();
        risk.setName(username);
        risk.setPsychological_score(score);

        LambdaQueryWrapper<Risk> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(Risk::getName,username);
        long count = riskMapper.selectCount(qw1);
        if (count >0){
            riskMapper.update(risk,qw1);
        }else {
            riskMapper.insert(risk);
        }

        return Result.success("success");
    }
}
